package com.letu.log.point;

import com.letu.core.context.ContextUtil;
import com.letu.core.point.*;
import com.letu.core.utils.HeaderUtil;
import com.letu.core.utils.SpringUtils;
import org.springframework.stereotype.Component;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * 埋点工具处理类
 *
 * @author ligang
 * @date 2021-8-06 11:13
 */
@Component
public class PointUtil {

    /**
     * 添加业务埋点信息
     *
     * @param obj
     */
    public void addBusinPointMsg(Object obj) {
        // TODO: 2022/1/21 发布业务事件
        BusinBasePoint businBasePoint = new BusinBasePoint();
        businBasePoint.setPointObj(obj);
        //组装header数据
        businBasePoint.setHeaderMap(HeaderUtil.getHeaderMap());
        //发送给applicatoin 事件发布
        SpringUtils.publishEvent(new BusinPointEvent(businBasePoint));
    }


    /**
     * 添加日志埋点信息
     *
     * @param obj
     */
    public void addLogPointMsg(Object obj) {
        // TODO: 2022/1/21 发布业务事件
        LogBasePoint logBasePoint = new LogBasePoint();
        logBasePoint.setPointObj(obj);
        //组装header数据
        logBasePoint.setHeaderMap(HeaderUtil.getHeaderMap());
        //发送给applicatoin 事件发布
        SpringUtils.publishEvent(new LogPointEvent(logBasePoint));
    }

    /**
     * 添加数据埋点信息
     *
     * @param obj
     */
    public void addDataPointMsg(Object obj) {
        // TODO: 2022/1/21 发布业务事件
        DataBasePoint dataBasePoint = new DataBasePoint();
        dataBasePoint.setPointObj(obj);
        //组装header数据
        dataBasePoint.setHeaderMap(HeaderUtil.getHeaderMap());
        //发送给applicatoin 事件发布
        SpringUtils.publishEvent(new DataPointEvent(dataBasePoint));
    }


    /**
     * 添加多个功能串联埋点信息
     *
     * @param obj
     */
    public void addOrderBusinPointMsg(Object obj) {
        // TODO: 2022/1/21 发布业务事件
        OrderBusinBasePoint orderBusinBasePoint = new OrderBusinBasePoint();
        orderBusinBasePoint.setPointObj(obj);
        //组装header数据
        orderBusinBasePoint.setHeaderMap(HeaderUtil.getHeaderMap());
        orderBusinBasePoint.setOrderPointOrderId(getOrderIdInvok(obj));
        //发送给applicatoin 事件发布
        SpringUtils.publishEvent(new OrderBusinPointEvent(orderBusinBasePoint));
    }

    /**
     * 添加消息中心埋点信息
     *
     * @param obj
     */
    public void addMessagePointMsg(Object obj) {
        // TODO: 2022/1/21 发布业务事件
        MessageBasePoint messageBasePoint = new MessageBasePoint();
        messageBasePoint.setPointObj(obj);
        //组装header数据
        messageBasePoint.setHeaderMap(HeaderUtil.getHeaderMap());
        //发送给applicatoin 事件发布
        SpringUtils.publishEvent(new MessagePointEvent(messageBasePoint));
    }
    /**
     * 添加广播埋点信息
     *
     * @param obj
     */
    public void addBroadPointMsg(Object obj) {
        // TODO: 2022/1/21 发布业务事件
        BroadBasePoint broadBasePoint = new BroadBasePoint();
        broadBasePoint.setPointObj(obj);
        //组装header数据
        broadBasePoint.setHeaderMap(HeaderUtil.getHeaderMap());
        //发送给applicatoin 事件发布
        SpringUtils.publishEvent(new BroadPointEvent(broadBasePoint));
    }


    /**
     * 获取排序orderid
     * @param object
     * @return
     */
    public static String getOrderIdInvok(Object object){
        Method m = null;
        try {
            m = object.getClass().getMethod("getOrderPointOrderId");
            Object pointObj = m.invoke(object);
            if(pointObj==null){
                return null;
            }
            return pointObj.toString();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        return null;
    }

}
